package com.eeepay.eeepay_v2.api;

import com.eeepay.eeepay_v2.model.UserInfo;
import com.eeepay.eeepay_v2.util.Constant;
import com.eeepay.eeepay_v2.util.GrpcUtils;

import java.util.concurrent.TimeUnit;

import cn.eeepay.api.grpc.nano.ManagementCenterProto;
import cn.eeepay.api.grpc.nano.ResMsgProto;
import cn.eeepay.api.grpc.nano.SuperPushServiceGrpc;
import io.grpc.ManagedChannel;

/**
 * 描述：新机具申请记录;处理机具
 * 作者：zhuangzeqin
 * 时间: 2017/8/11-11:44
 * 邮箱：zzq@eeepay.cn
 */
public class ModifyTerminalApplyRecordAPI {
    private ResultCallBack resultCallBack;//设置监听接口

    /**
     * 设置监听接口
     *
     * @param resultCallBack
     */
    public void setResultCallBack(ResultCallBack resultCallBack) {
        this.resultCallBack = resultCallBack;
    }

    /**
     * 处理机具申请记录
     * @param recordId 记录id
     * @param recordRemark 备注
     */
    public void getModifyTerminalApplyRecordInfo(final String recordId, final String recordRemark,final String merchantNo,final String terSn ) {
        if (resultCallBack == null) {
            throw new RuntimeException("ResultCallBack is null,you can must implement.");
        }

        GrpcUtils.getInstance().doGrpcTask(Constant.URL.URL, Constant.URL.PORT, 0, new GrpcUtils.AsyncTaskListener() {
            @Override
            public Object doInBack(ManagedChannel channel, int taskId) {
                SuperPushServiceGrpc.SuperPushServiceBlockingStub stub =
                        SuperPushServiceGrpc.newBlockingStub(channel).withDeadlineAfter(30, TimeUnit.SECONDS);
                ManagementCenterProto.ModifyTerminalApplyRecordReq recordReq = new ManagementCenterProto.ModifyTerminalApplyRecordReq();
                recordReq.recordId = recordId;
                recordReq.userId = UserInfo.getUserInfo2SP().getAgentNo();//调用查询的用户ID，代理商传代理商编号
                recordReq.recordRemark = recordRemark;//备注
                recordReq.merchantNo = merchantNo;//商户号
                recordReq.terSn = terSn;//sn 号
                recordReq.recordStatus = "1";// 提交过来的都是以处理的；状态：0:待直属处理  1:已处理  2:待一级处理
                return stub.modifyTerminalApplyRecord(recordReq);
            }

            @Override
            public void updateUI(Object result, int taskId) {
                if (result == null) {
                    return;
                }
                ResMsgProto.ResMsg resMsg = (ResMsgProto.ResMsg) result;
                if (!resMsg.status)
                    resultCallBack.onFail(resMsg.msg);
                else
                    resultCallBack.onSucess(resMsg.msg);
            }
        });
    }

    /**
     * 提供外围接口；通过接口回传
     */
    public interface ResultCallBack {
        void onFail(String msg);//失败返回；提示语

        void onSucess(String msg);//成功返回数据---待处理的机具数量
    }
}
